﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Toasts.Models
{
    public class Repository : DbContext
    {
        public DbSet<Toast> Toasts { get; set; }
        public DbSet<Submission> Submissions { get; set; }

        public Repository()
            : base("DefaultConnection")
        {
        }

        public void  saveEditedSubmissions(IEnumerable<Submission> smsns)
        {
            foreach (var submission in smsns)
            {
                switch (submission.Status)
                {
                    case "Pending":                        
                        this.Entry(submission).State = EntityState.Modified;
                        break;
                    case "Accepted":
                        this.Toasts.Add(new Toast { 
                            Occasion = submission.Occasion,
                            Origin = submission.Origin,
                            Content = submission.Content,
                            Author = submission.Author,
                            Website = submission.Website
                        });
                        this.Entry(submission).State = EntityState.Deleted;
                        break;
                    case "Rejected":
                        this.Entry(submission).State = EntityState.Deleted;
                        break;
                    default:
                        break;
                }
                this.SaveChanges();
                
            }
             
        }

        public void saveEditedSubmissionsDemo(IEnumerable<Submission> smsns, string ip)
        {
            foreach (var submission in smsns)
            {
                switch (submission.Status)
                {
                    case "Pending":
                        this.Entry(submission).State = EntityState.Modified;
                        break;
                    case "Accepted":
                        this.Toasts.Add(new Toast
                        {
                            Occasion = submission.Occasion,
                            Origin = submission.Origin,
                            Content = submission.Content,
                            Author = submission.Author,
                            Website = submission.Website
                        });
                        this.Entry(submission).State = EntityState.Deleted;
                        break;
                    case "Rejected":
                        this.Entry(submission).State = EntityState.Deleted;
                        break;
                    default:
                        break;
                }
                this.SaveChanges();

            }

        }
    }
}